package util;

import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.ImmutableTriple;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * 对象工具类
 * @author wevil
 * @date 2019/12/12
 * */
public class ObjectUtil {

    /**
     * 返回包含两个参数的临时对象
     *
     * @author wanglei
     * @date 2021/6/25
     * @return 获取数据方式：pair.getLeft() + "," + pair.getRight()
     * */
    public static ImmutablePair package2param(Object obj1, Object obj2){
        return ImmutablePair.of(obj1, obj2);
    }

    /**
     * 返回包含三个参数的临时对象
     *
     * @author wanglei
     * @date 2021/6/25
     * @return 获取数据方式：triple.getLeft() + "," + triple.getMiddle() + "," + triple.getRight()
     * */
    public static ImmutableTriple package3param(Object obj1, Object obj2, Object obj3){
        return ImmutableTriple.of(obj1, obj2, obj3);
    }

    /**
     * 对象转map
     *
     * @author wanglei
     * @date 2021/6/25
     * */
    public static Map<String, String> obj2map(Object bean) {
        try {
            return org.apache.commons.beanutils.BeanUtils.describe(bean);
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * map转对象
     *
     * @author wanglei
     * @date 2021/6/25
     * */
    public static void map2obj(Map<String, Object> map, Object obj){
        try {
            org.apache.commons.beanutils.BeanUtils.populate(obj, map);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
